package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.IWeChetService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信业务接口
 * @author zbr
 * @version 1.0
 * @since 2025/3/3 17:06
 */
@Service
public class WeChetServiceImpl implements IWeChetService {

    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${wechat.appid}")
    private String appid;

    @Value("${wechat.secret}")
    private String secret;

    /**
     * 获取WeChat基本参数
     * @return params
     */
    private Map<String, Object> getWeChatConfig() {
        Map<String, Object> params = new HashMap<>();
        params.put("appid", appid);
        params.put("secret", secret);
        return params;
    }

    /**
     * 根据code获取openid
     *
     * @param code 获取openid凭证
     * @return openid
     */
    @Override
    public String getOpenId(String code) {
        Map<String, Object> params = getWeChatConfig();
        params.put("js_code", code);
        String loginJson = HttpUtil.get(REQUEST_URL, params);
        if (ObjUtil.isNotEmpty(loginJson)) {
            JSONObject jsonObject = JSONUtil.parseObj(loginJson);
            if (ObjUtil.isNotEmpty(jsonObject) && ObjUtil.isEmpty(jsonObject.get("errcode"))) {
                return jsonObject.get("openid").toString();
            }
        }
        throw new BaseException("获取openid失败");
    }

    /**
     * 根据detailCode获取手机号
     *
     * @param detailCode 获取phoneNumber凭证
     * @return phoneNumber
     */
    @Override
    public String getPhoneNumber(String detailCode) {
        String token = getToken();
        Map<String, Object> params = Map.of("code", detailCode);
        String json = HttpUtil.post(PHONE_REQUEST_URL + token, JSONUtil.toJsonStr(params));
        if (ObjUtil.isNotEmpty(json)) {
            JSONObject jsonObject = JSONUtil.parseObj(json);
            if (ObjUtil.isNotEmpty(jsonObject) && ObjUtil.equal(jsonObject.get("errcode"),0)) {
                return jsonObject.getJSONObject("phone_info").get("phoneNumber").toString();
            }
        }
        throw new BaseException("获取phoneNumber失败");
    }

    /**
     * 获取access_token
     * @return token
     */
    private String getToken() {
        Map<String, Object> params = getWeChatConfig();
        String json = HttpUtil.get(TOKEN_URL, params);
        if (ObjUtil.isNotEmpty(json)) {
            JSONObject jsonObject = JSONUtil.parseObj(json);
            if (ObjUtil.isNotEmpty(jsonObject)) {
                return jsonObject.get("access_token").toString();
            }
        }
        throw new BaseException("获取access_token失败");
    }
}
